package database;

import java.util.*;

import javax.swing.text.html.HTMLDocument.Iterator;

import sharedspace.*;
/**
 * Responsible for data transition from SQL server to the server
 * @author G3
 *
 */
public class SubjectControl {
	
	
	private Subject subject = null;
	private List<Subject> list = null;
	
	public SubjectControl(){
		
	}
	/**
	 * Generate an array list with all subjects
	 * @return ArrayList<Subject> list
	 */
	public List<Subject> getSubjectList(){
		
		int subjectSize = JDBC.countStatment("SELECT count(*) FROM subject s;");
		list = new ArrayList<Subject>();
		Object[][] result = new Object[subjectSize][Subject.NUM_OF_COLUMNS];
		JDBC.selectStatmentMatrix("SELECT * FROM subject s", result, Subject.NUM_OF_COLUMNS, subjectSize);
		for(int i = 0; i<subjectSize; i++)
		{
			subject = new Subject();
			setSubject(result,i);
			list.add(subject);
		}
		return list;
	}
	/**
	 * Returns all subjects that a specific teacher belongs to
	 * @param teacherID
	 * @return ArrayList<Subject> list
	 */
	public List<Subject> getSubjectListByTeacherID(String teacherID)
	{
		int teacherInSubjectSize = JDBC.countStatment("SELECT count(*) FROM teacherinsubject tis where tis.userID=" + teacherID + ";");
		//subject = new Subject();
		list = new ArrayList<Subject>();
		Object[][] result = new Object[teacherInSubjectSize][1];
		JDBC.selectStatmentMatrix("SELECT tis.subjectID FROM teacherinsubject tis where tis.userID=" + teacherID + ";",result, 1, teacherInSubjectSize);
		for(int i = 0; i<teacherInSubjectSize; i++)
		{
			subject = new Subject();
			subject = getSubjectBySubjectID(result[i][0].toString());
			list.add(subject);
		}
		return list;
	}
	
	/**
	 * Create new subject and set its values according to the data in row in matrix[row][column] result
	 * @param result
	 * @param row
	 */
	public void setSubject(Object[][] result, int row)
	{
		subject = new Subject();
		subject.setSubjectID(result[row][0].toString());
		subject.setSubjectName(result[row][1].toString());

	}
	/**
	 * Create new subject and set its values according to the data  result[column]
	 * @param result
	 */
	public void setSubject(Object[] result)
	{
		subject = new Subject();
		subject.setSubjectID(result[0].toString());
		subject.setSubjectName(result[1].toString());
	}
	/**
	 * Returns an entire subject instance which its ID equals to sbjectID
	 * @param subjectID
	 * @return Subject subject
	 */
	public Subject getSubjectBySubjectID(String subjectID)
	{
		subject = new Subject();
		Object[] result = new Object[Subject.NUM_OF_COLUMNS];
		JDBC.selectStatment("SELECT * FROM subject s WHERE s.subjectID=" + subjectID + ";", result, Subject.NUM_OF_COLUMNS);
		setSubject(result);
		return subject;
		
	}

	 /*public static void main(String[] args) //debug code
	 {
		 SubjectControl S = new SubjectControl();
		 Subject S1;
		 S1 = S.getSubjectBySubjectID("01");
		 System.out.println(S1.getSubjectID()+" "+S1.getSubjectName());
		 List<Subject> list = new ArrayList<Subject>();
		 list = S.getSubjectListByTeacherID("036855336");
		 for (int i=0;i<list.size();i++)
		 {
			 S1 = list.get(i);
			 System.out.println(S1.getSubjectID()+" "+S1.getSubjectName());
		 }
		 
	 }*/



}
